﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public enum UILayer 
{
    Scene,
    Touch,
    FightUI,
    Normal,
    Top,
}

public class UIMgr : BaseManager<UIMgr>
{
    public bool UIEventSystemEnable
    {
        set { _event.enabled = value; }
        get { return _event.enabled; }
    }
    GameObject _uiRoot;
    EventSystem _event;
    //层次，根节点
    Dictionary<UILayer, GameObject> _uiLayerRoot = new Dictionary<UILayer, GameObject>();
    public void Init()
    {
        if (_uiRoot == null)
        {
            _uiRoot = ResManager.GetInstance().load<GameObject>("UI/UISystem");
            GameObject.DontDestroyOnLoad(_uiRoot);
            _event = _uiRoot.Find<EventSystem>("EventSystem");
        }
        
        _uiLayerRoot.Add(UILayer.Scene,_uiRoot.Find<Transform>("Scene").gameObject);
        _uiLayerRoot.Add(UILayer.Touch, _uiRoot.Find<Transform>("Touch").gameObject);
        _uiLayerRoot.Add(UILayer.FightUI, _uiRoot.Find<Transform>("FightUI").gameObject);
        _uiLayerRoot.Add(UILayer.Normal, _uiRoot.Find<Transform>("Normal").gameObject);
        _uiLayerRoot.Add(UILayer.Top, _uiRoot.Find<Transform>("Top").gameObject);
    }
    public GameObject Add(string uiPath,UILayer layer=UILayer.Normal)
    {
        var root= ResManager.GetInstance().load<GameObject>(uiPath);
        root.transform.SetParent(_uiLayerRoot[layer].transform, false);
        return root;
    }
    //删除某一层的所有UI
    public void RemoveLayer(UILayer layer =UILayer.Normal)
    {
        _uiLayerRoot[layer].DestroyAllChildren();
    }
  
    public GameObject Replace(string uiPath, UILayer layer=UILayer.Normal)
    {
        RemoveLayer(layer);
        return Add(uiPath, layer);
    }
    public void Remove(GameObject ui)
    {
        ResManager.GetInstance().Release(ui);
    }
}
